package org.jeecg.modules.yw.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.yw.entity.EnterPrise;
import org.jeecg.modules.yw.entity.MeasureAppoint;
import org.jeecg.modules.yw.entity.Rulerpaper;
import org.jeecg.modules.yw.mapper.MeasureAppointMapper;
import org.jeecg.modules.yw.result.Result;
import org.jeecg.modules.yw.utils.DateUtils;
import org.jeecg.modules.yw.vo.MeasureAppointV;
import org.jeecg.modules.yw.vo.MeasureAppointVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Validated
public class MeasureAppointService extends ServiceImpl<MeasureAppointMapper, MeasureAppoint> {
    @Autowired
    private IRulerpaperService rulerpaperService;
    @Autowired
    private SysAnnoucementService sysAnnoucementService;
    @Autowired
    private IWorkerCompanyRelationService workRelationService;
    
    @Autowired
    private EnterPriseService enterpriseService;

    public Result addAppointment(MeasureAppointVo measureAppointVo, String id){
        if(StringUtils.isBlank(measureAppointVo.getContactAddress()) || StringUtils.isBlank(measureAppointVo.getContactName())||
                StringUtils.isBlank(measureAppointVo.getContactPhone()) || StringUtils.isBlank(measureAppointVo.getMeasurementArea())||
                measureAppointVo.getAppointmentTime() == null || measureAppointVo.getAppointmentTime().isEmpty()){
            return Result.error("预约信息不完整");
        }
        // 获取预约日期的开始和结束时间
//        String appointmentTimeStr = measureAppointVo.getAppointment_time();
//        //获得库里预约时间的日期格式为Datetime
//        Date strToDateTime = DateUtils.strToDate(appointmentTimeStr);
//        // 库里的预约时间的开始时间和结束时间 类型为String
//        String todayStartTimeStr = DateUtils.getDayStartTimeStr(strToDateTime);
//        String todayEndTimeStr = DateUtils.getDayEndTimeStr(strToDateTime);
//         检查今天是否已经预约
//        QueryWrapper<MeasureAppoint> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("creator", id)
//                .between("appointment_time", todayStartTimeStr, todayEndTimeStr);
//        if (this.count(queryWrapper) > 0) {
//            return Result.error("您预约的时间已经预约量尺，请勿重复预约");
//        }
        // 复制前端传来的信息
        MeasureAppoint measureAppoint = new MeasureAppoint();
        BeanUtils.copyProperties(measureAppointVo, measureAppoint);
        measureAppoint.setEnterPriseId(id);
        
        EnterPrise enterPrise = enterpriseService.getById(id);
        // 设置创建者和创建时间
        measureAppoint.setCreator(id);
        measureAppoint.setCreateTime(DateUtils.getCurrentTimeDateStr());
        this.save(measureAppoint);
        String workerIds = workRelationService.getWorkersByCompanyId(id);
        sysAnnoucementService.AddSysAnnoucement("新预约量尺，请审核", "用户手机号:"+measureAppointVo.getContactPhone()+",新预约量尺，请审核", "19", id, workerIds,id, null,measureAppoint.getId().toString(), enterPrise.getCityCode());
        return Result.success("下单成功");
    };

    public IPage<MeasureAppointV> appointPage(IPage<MeasureAppointV> page, Date startDate, Date endDate,String[] areas, String[] enters) {
        IPage<MeasureAppointV> pageO = this.baseMapper.appointPage(page, startDate, endDate,areas,enters);
        for (MeasureAppointV order:pageO.getRecords()){
//		    EnterPrise enterPrise =	enterPriseService.getBaseMapper().selectOne(new QueryWrapper<EnterPrise>().eq("id",order.getCreateUserId()));
//			order.setEnterpriseName(enterPrise.getName());
            QueryWrapper<Rulerpaper> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("orderid",order.getId());
            queryWrapper1.orderByAsc("sorted");
            List<Rulerpaper> rlist =rulerpaperService.list(queryWrapper1);
            List<String> list = new ArrayList<>();
            for (Rulerpaper r:rlist){
                list.add(r.getPageurl());
            }
            order.setRList(list);
        }
        return pageO;
    }

    public IPage<MeasureAppoint> getpageList(Integer pageNo, Integer pageSize, String id) {
        QueryWrapper<MeasureAppoint> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterPriseId", id);
        queryWrapper.orderByDesc("create_time");
        IPage<MeasureAppoint> page = new Page<>(pageNo, pageSize);
        IPage<MeasureAppoint> resultPage = this.page(page, queryWrapper);
        return resultPage;
    }
}
